/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;
import Data.UseCaseStruct;
import Data.UseCaseModel;
import gui.*;
import java.awt.Color;
import javax.swing.*;
import java.io.*;
import javax.swing.table.*;

/**
 *
 * @author Sean
 */
public class UseCaseController {

    public static UseCaseStruct myUC;
    public static UseCaseModel myUCModel = new UseCaseModel();    
    
    public static void UseCaseController(){        
        return;
    }


    public static void Open(JTextPane reqTextPane, JButton automaticColoringButton){
        reqTextPane.setEnabled(true);
        JFrame frame = new JFrame();
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File newFile = chooser.getSelectedFile();
            try{
                // Open the file that is the first
                // command line parameter
                FileInputStream fstream = new FileInputStream(newFile);
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null)   {
                  // Print the content on the console
                  //System.out.println (strLine);
                  reqTextPane.replaceSelection(strLine);
                  reqTextPane.replaceSelection("\n");
                }
                //Close the input stream
                in.close();
                
                //enable button
                automaticColoringButton.setEnabled(true);
              }
            catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        }
    }

    public static void Enable(JTextPane reqTextPane){
        reqTextPane.setEnabled(true);
    }

    public static void startUseCase(JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, DefaultTableModel model, JTable useCaseTable,JButton nounButton, JButton automaticColoringButton){
        UseCaseGUI.incUseCaseCount();
        myUC = new UseCaseStruct();
        myUC.setId(UseCaseGUI.getUseCaseCount());
        model.addRow(new Object[]{"","","",false});
        actorButton.setEnabled(true);  //enable actor button
        verbButton.setEnabled(true);  //enable verb noun phrase button
        systemButton.setEnabled(true);  //enable system button
        nounButton.setEnabled(true);
        deriveButton.setEnabled(false);
        automaticColoringButton.setEnabled(true);

    }

    public static void actorPressed(JTextPane reqTextPane, JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, JTable useCaseTable, UseCaseGUI frame, JButton nounButton){
        String actor = reqTextPane.getSelectedText();
        int curLocation = reqTextPane.getSelectionStart();
        if(actor == null){
            JOptionPane.showMessageDialog(frame, "Invalid selction for actor.");
            reqTextPane.select(-1, -1);
            reqTextPane.setCaretPosition(curLocation);
            return;
        }
        myUC.setActor(actor.trim());
        if(!systemButton.isEnabled() && !verbButton.isEnabled() && !nounButton.isEnabled())
        {
            int answer = JOptionPane.showConfirmDialog(
                frame,
                "Abstraction of a business process?\n" +
                "Initiated by an actor?\n" +
                "Accomplishes some business task for the actor?\n" +
                "Ends with the actor?",
                "Verify",
                JOptionPane.YES_NO_OPTION);
            if(answer == JOptionPane.YES_OPTION) {
                myUC.setVerified(true);
                myUC.ConfigVNPhrase();
                myUCModel.add(myUC);
                useCaseTable.setValueAt(myUC.getActor(), UseCaseGUI.getUseCaseCount(), 0);
                useCaseTable.setValueAt(myUC.getVNPhrase(), UseCaseGUI.getUseCaseCount(), 1);
                useCaseTable.setValueAt(myUC.getSystem(), UseCaseGUI.getUseCaseCount(), 2);
                useCaseTable.setValueAt(myUC.isVerified(), UseCaseGUI.getUseCaseCount(), 3);               
                deriveButton.setEnabled(true);                
            } else if (answer == JOptionPane.NO_OPTION) {
                myUC.setVerified(false);
                myUC.ConfigVNPhrase();
                myUCModel.add(myUC);
                useCaseTable.setValueAt(myUC.getActor(), UseCaseGUI.getUseCaseCount(), 0);
                useCaseTable.setValueAt(myUC.getVNPhrase(), UseCaseGUI.getUseCaseCount(), 1);
                useCaseTable.setValueAt(myUC.getSystem(), UseCaseGUI.getUseCaseCount(), 2);
                useCaseTable.setValueAt(myUC.isVerified(), UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);               
            }
        }        
        actorButton.setEnabled(false);
       
        if (!reqTextPane.getSelectedText().equals("")) {
            ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.green);
        }
        reqTextPane.select(-1, -1);
        reqTextPane.setCaretPosition(curLocation);
        reqTextPane.select(curLocation, curLocation);
        ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.BLACK);
    }

    public static void verbPressed(JTextPane reqTextPane, JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, JTable useCaseTable, UseCaseGUI frame, JButton nounButton){
        String verb = reqTextPane.getSelectedText();
        int curLocation = reqTextPane.getSelectionStart();
        if(verb == null){
            JOptionPane.showMessageDialog(frame, "Invalid selection for verb.");
            reqTextPane.select(-1, -1);
            reqTextPane.setCaretPosition(curLocation);
            return;
        }
        myUC.setVerb(verb.trim());
        if(!actorButton.isEnabled() && !systemButton.isEnabled() && !nounButton.isEnabled())
        {
            int answer = JOptionPane.showConfirmDialog(
                frame,
                "Abstraction of a business process?\n" +
                "Initiated by an actor?\n" +
                "Accomplishes some business task for the actor?\n" +
                "Ends with the actor?",
                "Verify",
                JOptionPane.YES_NO_OPTION);
            if (answer == JOptionPane.YES_OPTION) {
                myUC.setVerified(true);
                myUC.ConfigVNPhrase();
                myUCModel.add(myUC);
                useCaseTable.setValueAt(myUC.getActor(), UseCaseGUI.getUseCaseCount(), 0);
                useCaseTable.setValueAt(myUC.getVNPhrase(), UseCaseGUI.getUseCaseCount(), 1);
                useCaseTable.setValueAt(myUC.getSystem(), UseCaseGUI.getUseCaseCount(), 2);
                useCaseTable.setValueAt(myUC.isVerified(), UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);               
            } else if (answer == JOptionPane.NO_OPTION) {
                myUC.setVerified(false);
                myUC.ConfigVNPhrase();
                myUCModel.add(myUC);
                useCaseTable.setValueAt(myUC.getActor(), UseCaseGUI.getUseCaseCount(), 0);
                useCaseTable.setValueAt(myUC.getVNPhrase(), UseCaseGUI.getUseCaseCount(), 1);
                useCaseTable.setValueAt(myUC.getSystem(), UseCaseGUI.getUseCaseCount(), 2);
                useCaseTable.setValueAt(myUC.isVerified(), UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);
            }
        }
        verbButton.setEnabled(false);
        if (!reqTextPane.getSelectedText().equals("")) {
            ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.red);
        }
        reqTextPane.select(-1, -1);
        reqTextPane.setCaretPosition(curLocation);
        reqTextPane.select(curLocation, curLocation);
        ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.BLACK);
    }

    public static void nounPressed(JTextPane reqTextPane, JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, JTable useCaseTable, UseCaseGUI frame, JButton nounButton){
        String noun = reqTextPane.getSelectedText();
        int curLocation = reqTextPane.getSelectionStart();
        if(noun == null){
            JOptionPane.showMessageDialog(frame, "Invalid selction for noun.");
            reqTextPane.select(-1, -1);
            reqTextPane.setCaretPosition(curLocation);
            return;
        }
        myUC.setNoun(noun.trim());
        if(!actorButton.isEnabled() && !systemButton.isEnabled() && !verbButton.isEnabled())
        {
            int answer = JOptionPane.showConfirmDialog(
                frame,
                "Abstraction of a business process?\n" +
                "Initiated by an actor?\n" +
                "Accomplishes some business task for the actor?\n" +
                "Ends with the actor?",
                "Verify",
                JOptionPane.YES_NO_OPTION);
            if (answer == JOptionPane.YES_OPTION) {
                myUC.setVerified(true);
                myUC.ConfigVNPhrase();
                myUCModel.add(myUC);
                useCaseTable.setValueAt(myUC.getActor(), UseCaseGUI.getUseCaseCount(), 0);
                useCaseTable.setValueAt(myUC.getVNPhrase(), UseCaseGUI.getUseCaseCount(), 1);
                useCaseTable.setValueAt(myUC.getSystem(), UseCaseGUI.getUseCaseCount(), 2);
                useCaseTable.setValueAt(myUC.isVerified(), UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);
            } else if (answer == JOptionPane.NO_OPTION) {
                myUC.setVerified(false);
                myUC.ConfigVNPhrase();
                myUCModel.add(myUC);
                useCaseTable.setValueAt(myUC.getActor(), UseCaseGUI.getUseCaseCount(), 0);
                useCaseTable.setValueAt(myUC.getVNPhrase(), UseCaseGUI.getUseCaseCount(), 1);
                useCaseTable.setValueAt(myUC.getSystem(), UseCaseGUI.getUseCaseCount(), 2);
                useCaseTable.setValueAt(myUC.isVerified(), UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);
            }
        }
        nounButton.setEnabled(false);        
        if (!reqTextPane.getSelectedText().equals("")) {
            ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.red);
        }
        reqTextPane.select(-1, -1);
        reqTextPane.setCaretPosition(curLocation);
        reqTextPane.select(curLocation, curLocation);
        ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.BLACK);
    }

    public static void systemPressed(JTextPane reqTextPane, JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, JTable useCaseTable, UseCaseGUI frame, JButton nounButton){
        String system = reqTextPane.getSelectedText();
        int curLocation = reqTextPane.getSelectionStart();
        if(system == null){
            JOptionPane.showMessageDialog(frame, "Invalid selection for system.");
            reqTextPane.select(-1, -1);
            reqTextPane.setCaretPosition(curLocation);
            return;
        }
        myUC.setSystem(system.trim());
        if(!actorButton.isEnabled() && !verbButton.isEnabled() && !nounButton.isEnabled())
        {
            int answer = JOptionPane.showConfirmDialog(
                frame,
                "Abstraction of a business process?\n" +
                "Initiated by an actor?\n" +
                "Accomplishes some business task for the actor?\n" +
                "Ends with the actor?",
                "Verify",
                JOptionPane.YES_NO_OPTION);
            if (answer == JOptionPane.YES_OPTION) {
                myUC.setVerified(true);
                myUC.ConfigVNPhrase();
                myUCModel.add(myUC);
                useCaseTable.setValueAt(myUC.getActor(), UseCaseGUI.getUseCaseCount(), 0);
                useCaseTable.setValueAt(myUC.getVNPhrase(), UseCaseGUI.getUseCaseCount(), 1);
                useCaseTable.setValueAt(myUC.getSystem(), UseCaseGUI.getUseCaseCount(), 2);
                useCaseTable.setValueAt(myUC.isVerified(), UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);                
            } else if (answer == JOptionPane.NO_OPTION) {
                myUC.setVerified(false);
                myUC.ConfigVNPhrase();
                myUCModel.add(myUC);
                useCaseTable.setValueAt(myUC.getActor(), UseCaseGUI.getUseCaseCount(), 0);
                useCaseTable.setValueAt(myUC.getVNPhrase(), UseCaseGUI.getUseCaseCount(), 1);
                useCaseTable.setValueAt(myUC.getSystem(), UseCaseGUI.getUseCaseCount(), 2);
                useCaseTable.setValueAt(myUC.isVerified(), UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);

            }             
        }        
        systemButton.setEnabled(false);           
        if (!reqTextPane.getSelectedText().equals("")) {
            ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.blue);            
        }                
        reqTextPane.select(-1, -1);
        reqTextPane.setCaretPosition(curLocation);
        reqTextPane.select(curLocation, curLocation);
        ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.BLACK);
    }

    public static void updateFromTablePressed( JTable useCaseTable ){
        useCaseTable.enableInputMethods(true);
        myUCModel.getUseCaseVector().removeAllElements();              
        for(int i = 0; i < useCaseTable.getRowCount(); i++){
            myUC = new UseCaseStruct();
            myUC.setNoun("");
            myUC.setVerb("");
            myUC.setId(i);
            //System.out.print(useCaseTable.getCellEditor(i, 0).getCellEditorValue().toString());
            myUC.setActor(useCaseTable.getValueAt(i, 0).toString());
            myUC.setSystem(useCaseTable.getValueAt(i, 2).toString());
            myUC.setvNPhrase(useCaseTable.getValueAt(i, 1).toString());
            myUC.setVerified(useCaseTable.getValueAt(i, 3).toString());
            if(myUC.isVerified()){
                myUCModel.add(myUC);
            }
        }
    }

    

}
